import React from 'react';
import ReactDOM from 'react-dom';
import './static/main.css';
import $ from 'jquery'
import go from './utils/global';
import Header from './component/Header.js';
import Chat from './component/Chat.js';
import SearchResult from './component/SearchResult.js';
import Parser from './component/Parser.js';

class App extends React.Component{
  constructor(props){
    super(props)
    this.state = {
      //搜索结果数据-累积
      searchData:{},
      //当前搜索项
      curSearchData:{},
      //解析结果数据
      parserData:{},
      //当前搜解析项
      curParserData:{},
      pageName:'search'
      // pageName:'parser'
    }
  }
  componentDidMount() {
    // console.log(this.props);


  }

  //显示搜索页面
  showSearchPage = (uid)=>{
    let searchData = go.copy(this.state.searchData)
    searchData[uid].isHide = false
    this.setState({pageName:'search',searchData:searchData, curSearchData:{key:uid,value:searchData[uid]}})
  }

  //显示解析页面
  showParserPage = (uid)=>{
    let parserData = go.copy(this.state.parserData)
    parserData[uid].isHide = false
    this.setState({pageName:'parser',parserData:parserData, curParserData:{key:uid,value:parserData[uid]}})
  }

  /////////////////////////////////////////////////搜索结果START////////////////////////////////////////////////////////////
  //创建搜索结果数据-Chat组件使用
  createSearchData = (key,value)=>{
    // console.log(key,value)
    let items = go.copy(this.state.searchData)
    // console.log(items)
    items[key] = value
    this.setState({searchData:items,curSearchData:{key:key,value:value}})
  }

  //修改当前搜索数据项-SearchResult组件使用
  changeCurSearchData = (obj)=>{
    obj = obj ? obj : {}
    let ckey = obj.key ? obj.key : this.state.curSearchData.key
    if(obj.type === 'delete'){
      //删除tab

      //如果只有一个tab,不允许删除
      // let count = 0
      // for(let k in this.state.searchData){
      //   count++
      //   if(k){}
      // }
      // if(count < 2){
      //   return
      // }

      let items = go.copy(this.state.searchData)
      // console.log('items', items)

      //删除累加数据-软删除
      // delete items[ckey]
      items[ckey].isHide = true

      //替换当前数据
      let curData = {}
      for(let index in items){
        if(!items[index].isHide){
          curData = {key:index,value:items[index]}
          break;
        }
      }
      // console.log('ckey', ckey)

      this.setState({searchData:items,curSearchData:curData})
    }else{
      this.setState({curSearchData:{key:ckey,value:obj.value}})
    }
  }
  /////////////////////////////////////////////////搜索结果END////////////////////////////////////////////////////////////



  /////////////////////////////////////////////////解析结果START////////////////////////////////////////////////////////////
  // createParserData = (data)=>{
  //   this.setState({parserData:data})
  // }
  //创建解析结果数据-Chat组件使用
  createParserData = (key,value)=>{
    // console.log(key,value)
    let items = go.copy(this.state.parserData)
    // console.log(items)
    items[key] = value
    this.setState({parserData:items,curParserData:{key:key,value:value}})
  }

  //修改当前解析数据项-SearchResult组件使用
  changeCurParserData = (obj)=>{
    obj = obj ? obj : {}
    let ckey = obj.key ? obj.key : this.state.curParserData.key
    // console.log(ckey)

    //修改知识推断,必传参数key、value、type、infer_node
    if(obj.type === 'infer'){
      let curParserData = go.copy(this.state.curParserData)
      let cur_infer = curParserData.value.word_slot[obj.word_slot_key].words[obj.word_key].infer
      //去重
      if(!$.isEmptyObject(cur_infer)){
        for(let key in cur_infer){
          if (!$.isEmptyObject(cur_infer[key]) && cur_infer[key].node === obj.infer_node) {
            return
          }
        }
      }

      //更新当前
      // console.log(obj.word_slot_key)
      cur_infer.push({"node":obj.infer_node,"relation":"？","type":"add"})

      //更新累加
      let items = go.copy(this.state.parserData)
      items[ckey] = curParserData.value

      this.setState({parserData:items,curParserData:curParserData})
    }else if(obj.type === 'word_type'){
      //////修改词属性///////
      let curParserData = go.copy(this.state.curParserData)
      let from = obj.from
      let to = obj.to

      if(!obj.from){
        console.log('word_type-log000')
        return
      }

      if(!curParserData.value.word_slot.length){
        console.log('word_type-log111')
        return
      }

      if(!curParserData.value.word_slot[from.word_slot_key]){
        console.log('word_type-log111')
        return
      }

      if(!curParserData.value.word_slot[from.word_slot_key].words[from.word_key]){
        console.log('word_type-log222')
        return
      }

      if(!curParserData.value.word_slot[to.word_slot_key]){
        console.log('word_type-log444')
        return
      }

      if(!curParserData.value){
        return
      }
      
      //更新当前(当前词移动到TO属性下面)
      //如果属性相同,不动
      if(to.word_slot_key === from.word_slot_key){
        return
      }

      let from_word = curParserData.value.word_slot[from.word_slot_key].words[from.word_key].text
      //已存在则不动
      curParserData.value.word_slot[to.word_slot_key].words.forEach(function(value){
        if(value.text === from_word){
          return
        }
      })

      //复制
      curParserData.value.word_slot[to.word_slot_key].words.push(curParserData.value.word_slot[from.word_slot_key].words[from.word_key])

      //删除当前
      delete curParserData.value.word_slot[from.word_slot_key].words[from.word_key]

      //更新累加
      let items = go.copy(this.state.parserData)
      items[ckey] = curParserData.value

      this.setState({parserData:items,curParserData:curParserData})
    }else if(obj.type === 'intent'){
      //////修改意图///////
      let curParserData = go.copy(this.state.curParserData)
      //更新当前
      curParserData.value.intent_name = obj.intent.value
      curParserData.value.intent = obj.intent.key
      //更新累加
      let items = go.copy(this.state.parserData)
      items[ckey] = curParserData.value

      this.setState({parserData:items,curParserData:curParserData})
    }else if(obj.type === 'delete'){
      //////删除tab///////
      //如果只有一个tab,不允许删除
      let count = 0
      for(let k in this.state.parserData){
        if(!this.state.parserData[k].isHide){
          count++
        }
        if(k){}
      }
      console.log('count', count)
      // return

      if(count < 2){
        return
      }

      let items = go.copy(this.state.parserData)
      console.log('items', items)
      // return

      //删除累加数据-软删除
      // delete items[ckey]
      items[ckey].isHide = true

      //替换当前数据
      let curData = {}
      for(let index in items){
        if(!items[index].isHide){
          curData = {key:index,value:items[index]}
          break;
        }
      }
      // console.log('ckey', ckey)

      this.setState({parserData:items,curParserData:curData})
    }else{
      //可以只传递一个value过来更新
      //更新累加
      let items = go.copy(this.state.parserData)
      items[ckey] = obj.value
      this.setState({parserData:items,curParserData:{key:ckey,value:obj.value}})
    }
  }
  /////////////////////////////////////////////////解析结果END////////////////////////////////////////////////////////////


  render () {
    let content;
    if (this.state.pageName === 'search') {
      content = <div className="search-result">
                <SearchResult 
                searchData={this.state.searchData} 
                curSearchData={this.state.curSearchData} 
                changeCurSearchData={this.changeCurSearchData}
                curParserData={this.state.curParserData} 
                />
                </div>
    } else if (this.state.pageName === 'parser') {
      content = <div className="parser-result">
                <Parser parserData={this.state.parserData} curParserData={this.state.curParserData} changeCurParserData={this.changeCurParserData}/>
                </div>
    }

    return (
      <div className="npl-box">
        <Header />
        <div className="main">
          <Chat 
          showSearchPage={this.showSearchPage} 
          showParserPage={this.showParserPage} 
          createSearchData={this.createSearchData} 
          createParserData={this.createParserData}
          />
          {content}
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));